import {
  createRouter,
  createWebHashHistory,
  type LocationQuery,
  type RouteParams,
  type RouteRecordRaw
} from 'vue-router';

declare module 'vue-router' {
  interface RouteMeta extends Record<PropertyKey, unknown> {
    // 是否在路由列表中展示
    showInList?: boolean,
    // 页面标题
    title?: string,
    // 开发模式主动传递给页面的query
    query?: LocationQuery,
    // 开发阶段主动传递给页面的params
    params?: RouteParams
  }
}

/* eslint-disable @typescript-eslint/promise-function-async */
export const routes: RouteRecordRaw[] = [];

if (process.env.NODE_ENV === 'development') {
  routes.push({
    path: '/',
    name: '导航页',
    meta: {
      showInList: false
    },
    component: () => import('./ViewSelector.vue')
  });
  const verifyRouter = [...routes];
  for (let i = routes.length - 1; i >= 0; i--) {
    const indexRouter = verifyRouter[i];
    verifyRouter.pop();
    if (verifyRouter.findIndex(x => x.name === indexRouter.name || x.path === indexRouter.path) > -1)
      alert(`路由名称或路径重复，name：${indexRouter.name as string}，path：${indexRouter.path}`);
  }
}
/* eslint-enable @typescript-eslint/promise-function-async */

const router = createRouter({
  routes,
  history: createWebHashHistory()
});

router.beforeEach((to, _from, next) => {
  if (typeof to.meta.title === 'string')
    document.title = to.meta.title;
  else if (typeof to.name === 'string')
    document.title = to.name;
  next();
});

export default router;
